﻿using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SmlEngine.Sprites.Base
{
    public sealed class BackgroundSet
    {
        public IList<BackgroundLayer> Layers { get; private set; }

        public BackgroundSet()
        {
            Layers = new List<BackgroundLayer>();
        }

        public void Update(GameTime gameTime, Vector2 delta)
        {
            Vector2 cumulativeVelocity = delta;

            foreach (BackgroundLayer layer in Layers)
            {
                Vector2 offset = layer.ScrollType == ScrollType.Linear ? 
                                                     cumulativeVelocity * layer.ScrollSpeed : 
                                                     cumulativeVelocity * layer.ScrollSpeed * layer.ScrollSpeed;

                layer.Offset += offset;
                cumulativeVelocity += offset;
            }
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch, int section)
        {
            spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearWrap, null, null);

            foreach (BackgroundLayer layer in Layers)
            {
                if (layer.Section == section)
                    layer.Draw(spriteBatch);
            }

            spriteBatch.End();
        }
    }
}
